<template>
  <el-form ref="projectInfoForm"
           :model="projectInfoForm"
           label-width="85px"
           size="small"
           :rules="rules">
    <el-row>
      <el-col :span="6">
        <el-form-item label="姓名:">
          <el-input v-model="projectInfoForm.pname"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="来源:">
          <el-input v-model="projectInfoForm.source"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="兴趣备注:">
          <el-input v-model="projectInfoForm.inmark"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="类别:"
                      prop="type">
          <el-select v-model="projectInfoForm.type"
                     placeholder="类别">
            <el-option v-for="item in options.type"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <!-- <el-col :span="12">
        <el-form-item label="客户名称:"
                      prop="customerName">
          <el-input v-model="projectInfoForm.customerName"></el-input>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="行业或方案产品:"
                      prop="hyPro">
          <el-input v-model="projectInfoForm.hyPro"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <!-- <el-col :span="6">
        <el-form-item label="原合同号:"
                      :prop="mode===1 ? 'contractId': ''">
          <el-input v-model="projectInfoForm.contractId"
                    :disabled="mode === 2"></el-input>
        </el-form-item>
      </el-col> -->
      <el-col :span="12">
        <el-form-item label="链接负责人:"
                      prop="fzr">
          <el-input v-model="projectInfoForm.fzr"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="省份:"
                      prop="pro">
          <el-input v-model="projectInfoForm.pro"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <!-- <el-col :span="6">
        <el-form-item label="到期时间:">
          <el-date-picker v-model="projectInfoForm.endTime"
                          type="date"
                          style="width: 150px"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-col> -->
      <!-- <el-col :span="6">
        <el-form-item label="成熟度:"
                      prop="projectMaturity">
          <el-select v-model="projectInfoForm.projectMaturity"
                     placeholder="成熟度"
                     :disabled="maturity === '已签'">
            <el-option v-for="item in options.projectMaturity"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col> -->
      <el-col :span="6">
        <el-form-item label="分布:"
                      prop="contractType">
          <el-select v-model="projectInfoForm.contractType"
                     placeholder="分布">
            <el-option v-for="item in options.contractType"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="类型方向:"
                      prop="contractDirection">
          <el-select v-model="projectInfoForm.contractDirection"
                     placeholder="类型方向">
            <el-option v-for="item in options.contractDirection"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-form-item label="主跟进人:"
                      prop="trackPepole">
          <el-input v-model="projectInfoForm.trackPepole"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="计划签订:"
                      prop="contractDate"
                      :disabled="maturity === '已签'">
          <el-date-picker v-model="projectInfoForm.contractDate"
                          type="month"
                          :disabled="mode === 2"
                          style="width: 140px"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="预估金额:"
                      prop="estimatedAmount">
          <el-input v-model="projectInfoForm.estimatedAmount"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="备案:">
          <el-radio-group v-model="projectInfoForm.isReport">
            <el-radio v-for="item in options.isReport"
                      :label="item.value"
                      :key="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-form-item label="预算:">
          <el-radio-group v-model="projectInfoForm.isBudget">
            <el-radio v-for="item in options.isBudget"
                      :label="item.value"
                      :key="item.value">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="6"
              v-if="projectInfoForm.isBudget">
        <el-form-item label="预算金额:"
                      prop="budgetAmount">
          <el-input v-model="projectInfoForm.budgetAmount"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="推进:"
                      prop="advancingState">
          <el-select v-model="projectInfoForm.advancingState"
                     placeholder="推进"
                     :disabled="maturity === '已签'">
            <el-option v-for="item in options.advancingState"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="跟踪等级:"
                      prop="projectLevel"
                      :disabled="maturity === '已签'">
          <el-select v-model="projectInfoForm.projectLevel"
                     placeholder="跟踪等级">
            <el-option v-for="item in options.projectLevel"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">
        <el-form-item label="签订类型:"
                      prop="signType">
          <el-radio-group v-model="projectInfoForm.signType">
            <el-radio v-for="item in options.signType"
                      :label="item.value"
                      :key="item.value">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="6"
              v-if="projectInfoForm.projectMaturity == '已签' || projectInfoForm.projectMaturity == '90%'">
        <el-form-item label="签订人员:"
                      prop="contractingOfficer">
          <el-input v-model="projectInfoForm.contractingOfficer"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6"
              v-if="projectInfoForm.projectMaturity == '已签' || projectInfoForm.projectMaturity == '90%'">
        <el-form-item label="签订金额:"
                      prop="signAmount">
          <el-input v-model="projectInfoForm.signAmount"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6"
              v-if="projectInfoForm.projectMaturity == '已签' || projectInfoForm.projectMaturity == '90%'">
        <el-form-item label="新合同号:"
                      :prop="mode===2 ? 'newContractId': ''">
          <el-input v-model="projectInfoForm.newContractId"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12"
              v-if="projectInfoForm.advancingState != '是'">
        <el-form-item label="失败原因:"
                      prop="causeFailure">
          <el-input v-model="projectInfoForm.causeFailure"
                    type="textarea"
                    :rows="2"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="备注情况:">
          <el-input v-model="projectInfoForm.remark"
                    type="textarea"
                    :rows="2"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24"
              v-if="mode===1">
        <p style="color:red">备注：新增项目，无合同号则自行新增合同号，例：GD180101001【省份(首字母大写2位)+年份(末2位)+月份(2位)+日期(2位)+编号(3位)】</p>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import Mement from 'moment'
import { mapGetters } from 'vuex'
import { getMInfoById, addMInfo, voidMById, addPlanProcess } from '@/api/maintenance'
export default {
  props: {
    contractId: {
      type: String
    },
    maturity: {
      type: String // 项目成熟度
    },
    mode: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      // 选项
      options: {
        // 责任区域
        territory: [],
        // 成熟度
        projectMaturity: [],
        // 分布
        contractType: [],
        // 类型方向
        contractDirection: [],
        // 是否备案
        isReport: [{
          value: true,
          label: '是'
        }, {
          value: false,
          label: '否'
        }],
        // 是否预算
        isBudget: [{
          value: true,
          label: '是'
        }, {
          value: false,
          label: '否'
        }],
        // 推进
        advancingState: [],
        // 跟踪等级
        projectLevel: [],
        // 签订类型
        signType: []
      },
      // 表单验证规则
      rules: {
        province: [ // 省
          { required: true, message: '请输入省份!', trigger: 'blur' }
        ],
        territory: [ // 责任区域
          { required: true, message: '请填选中区域!', trigger: 'blur' }
        ],
        customerName: [ // 客户名称
          { required: true, message: '请输入客户名称!', trigger: 'blur' }
        ],
        customerUnit: [ // 客户单位
          { required: true, message: '请输入客户单位!', trigger: 'blur' }
        ],
        contractName: [ // 项目名称
          { required: true, message: '请输入项目名称!', trigger: 'blur' }
        ],
        newContractId: [ // 项目名称
          { required: true, message: '请输入合同号!', trigger: 'blur' }
        ],
        contractId: [ // 合同号
          { required: true, message: '请输入合同号!', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (
                /^[A-Z]{2,2}\d{9,9}$/.test(value) === false &&
                /^\d{7,7}$/.test(value) === false
              ) {
                callback(new Error('编号格式不匹配,请看备注!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        contractAmount: [ // 合同金额
          { required: true, message: '请输入合同金额!', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^\d+(\.\d+)?$/.test(value) === false) {
                callback(new Error('金额需为数字,无则填0!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        projectMaturity: [ // 成熟度
          { required: true, message: '请选择成熟度!', trigger: 'blur' }
        ],
        contractType: [ // 分布
          { required: true, message: '请选择项目分布!', trigger: 'blur' }
        ],
        contractDirection: [ // 类型方向
          { required: true, message: '请选择类型方向!', trigger: 'blur' }
        ],
        trackPepole: [ // 主跟进人
          { required: true, message: '请输入主跟进人!', trigger: 'blur' }
        ],
        contractDate: [ // 计划签订
          { required: true, message: '请选择计划签订日期!', trigger: 'blur' }
        ],
        estimatedAmount: [ // 预估金额
          { required: true, message: '请输入预估金额!', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^\d+(\.\d+)?$/.test(value) === false) {
                callback(new Error('金额需为数字,无则填0!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        budgetAmount: [ // 预算金额
          { required: true, message: '请输入预算金额!', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^\d+(\.\d+)?$/.test(value) === false) {
                callback(new Error('金额需为数字,无则填0!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        advancingState: [ // 推进
          { required: true, message: '请选择推进状态!', trigger: 'blur' }
        ],
        projectLevel: [ // 跟踪等级
          { required: true, message: '请选择跟踪等级!', trigger: 'blur' }
        ],
        signType: [ // 签订类型
          { required: true, message: '请选择签订类型!', trigger: 'blur' }
        ],
        contractingOfficer: [ // 签订人员
          { required: true, message: '请输入签订人员!', trigger: 'blur' }
        ],
        signAmount: [ // 签订金额
          { required: true, message: '请输入签订金额!', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^\d+(\.\d+)?$/.test(value) === false || value === 0) {
                callback(new Error('金额需为数字/不能为0'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        causeFailure: [ // 失败原因
          { required: true, message: '请输入失败原因!', trigger: 'blur' }
        ]
      },
      // 表单数据
      projectInfoForm: {}
    }
  },
  computed: {
    ...mapGetters([
      'appEnum'
    ])
  },
  mounted() {
    this.initEnum()
  },
  methods: {
    // 根据合同号请求项目信息
    getProjectInfo() {
      // 初始化下拉菜单数据
      this.initEnum()
      // 请求接口数据
      getMInfoById({
        contractId: this.contractId
      }).then(res => {
        if (res.data.success) {
          const result = res.data.data[0]
          this.projectInfoForm = result
        }
      })
    },
    // 新增修改项目信息
    addProjectInfo() {
      this.$refs.projectInfoForm.validate(valid => {
        if (valid) {
          addMInfo(this.projectInfoForm).then(res => {
            if (res.data.success) {
              const result = res.data.data
              let message = '合同信息修改成功！'
              if (this.mode === 1) message = '新增合同信息成功！'
              if (result) {
                this.$message({
                  message: message,
                  type: 'success',
                  duration: 5 * 1000
                })
                // // 获取当前时间的日期
                // const nowDay = Mement().format('DD')
                // // 如果当前日期在25-下月2日之间,将计划签订日期保存到记录表中
                // if ((nowDay >= 25 || nowDay <= 2) && this.projectInfoForm !== '已签') this.addContractDateProcess()
                // // 通知父亲关闭
                this.$emit('close-detail')
              }
            }
          })
        }
      }
      )
    },
    // 验证服务合同号是否存在
    voidProjectInfo() {
      voidMById({
        contractId: this.contractId
      }).then(res => {
        if (res.data.success) {
          const result = res.data.data
          if (!result) {
            this.addProjectInfo()
          } else {
            this.$message({
              message: '合同号已存在!',
              type: 'error',
              duration: 5 * 1000
            })
          }
        }
      })
    },
    // 记录计划签订时间
    addContractDateProcess() {
      const data = {
        contractId: this.contractId,
        planMonth: Mement(this.projectInfoForm.contractDate).format('YYYY-MM')
      }
      addPlanProcess(data).then(res => {
        if (res.data.success) {
          const result = res.data.data
          if (result) {
            this.$message({
              message: '新增/修改项目月计划信息!',
              type: 'success',
              duration: 5 * 1000
            })
          }
        }
      })
    },
    // 重置验证
    fromResetField() {
      this.$refs.projectInfoForm.resetFields()
    },
    // 初始化下拉菜单
    initEnum() {
      this.options.projectMaturity = this.appEnum.maturity
      this.options.territory = this.appEnum.territory
      this.options.signType = this.appEnum.signType
      this.options.advancingState = this.appEnum.advancingState
      this.options.projectLevel = this.appEnum.projectLevel
      this.options.contractType = this.appEnum.contractType
      this.options.contractDirection = this.appEnum.direction
    }
  },
  watch: {
    // contractId() {
    //   // 合同号变动后获取项目信息
    //   if (this.contractId === 2) {
    //     this.getProjectInfo()
    //   } else {
    //     this.projectInfoForm = {}
    //   }
    // }
  }
}
</script>
